﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Dal
{
    public class OrderItemsInvoiceDao
    {        
        public static DataSet getOItemsInvoiceReport(string distributor, DateTime startDate, DateTime endDate, string ambient)        
        {
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }
           
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            DataSet ds = null;

            if (!distributor.Split('|').GetValue(1).Equals("ErrorReport"))
            {               
                //distributor.Split('|').GetValue(X) : para sacar parametro idDIstribuidor y Modo de ejecucion del Procedimiento almacenado
                cmd.CommandText = "dms_Report_ImportOrderItemInvoiceItem";
                cmd.Parameters.AddWithValue("@idDistributor", distributor.Split('|').GetValue(0));
                cmd.Parameters.AddWithValue("@dtInvoiceStart", startDate);
                cmd.Parameters.AddWithValue("@dtInvoiceEnd", endDate);
                cmd.Parameters.AddWithValue("@modo", distributor.Split('|').GetValue(1));

                cmd.CommandTimeout = 0;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("DT_ImportOrderItemInvoiceItem");

                try
                {
                    cmd.Connection.Open();
                    da.Fill(dt);
                    cmd.Connection.Close();
                }
                catch (Exception ex)
                {
                    Credentials.writeLogFile("getOItemsInvoiceReport: " + ex.Message);
                    return null;
                }
                ds = new DataSet("DS_ImportOrderItemInvoiceItem");
                ds.Tables.Add(dt);                               
            }
            else {
                cmd.CommandText = "dms_Report_ErrorsInvoiceDetailsInvoice";
                cmd.Parameters.AddWithValue("@idDistributor", distributor.Split('|').GetValue(0));
                cmd.Parameters.AddWithValue("@dtInvoiceStart", startDate);
                cmd.Parameters.AddWithValue("@dtInvoiceEnd", endDate);
                cmd.CommandTimeout = 0;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("DT_ErrorsInvoiceDetailsInvoice");

                try
                {
                    cmd.Connection.Open();
                    da.Fill(dt);
                    cmd.Connection.Close();
                }
                catch (Exception ex)
                {
                    return null;
                }
                ds = new DataSet("getErrorsInvoiceDetailsInvoice");
                ds.Tables.Add(dt);                
            }
            return ds;
        }
        
        public static DataSet getOItemsInvoiceByInvoiceReport(string distributor, DateTime startDate, DateTime endDate, string cdInvoice, string ambient)
        {
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);           
            cmd.CommandType = System.Data.CommandType.StoredProcedure;  
        
            cmd.CommandText = "dms_Report_ImportOrderItemInvoiceItem_ByInvoice";
            cmd.Parameters.AddWithValue("@idDistributor", distributor);
            cmd.Parameters.AddWithValue("@dtInvoiceStart", startDate);
            cmd.Parameters.AddWithValue("@dtInvoiceEnd", endDate);
            cmd.Parameters.AddWithValue("@cdInvoice", cdInvoice);
            cmd.CommandTimeout = 0;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("DT_ImportOrderItemInvoiceItem_ByInvoice");

            try
            {              
                    cmd.Connection.Open();
                    da.Fill(dt);
                    cmd.Connection.Close();               
            }
            catch (Exception ex)
            {
                return null;
            }
            DataSet ds = new DataSet("getOItemsInvoiceByInvoiceReport");            
            ds.Tables.Add(dt);
            
            return ds;          
        }       
    }
}
